package com.shiger.pingworld;

import android.app.Activity;
import android.database.sqlite.SQLiteConstraintException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity implements View.OnClickListener {


    private String TAG = this.getClass().getSimpleName();

    //
    ListView mListView;
    //
    Button mButtonRefesh;
    //
    List<String> mUrlList = new ArrayList<String>();
    //
    private EditText mEditTextIP;
    //
    private UserDao mUserDao;
    //
    ListViewAdapter mListViewAdapter;
    //
    private static final int MSG_DELE_DATA = 0;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case MSG_DELE_DATA:
                    int position = (int)msg.obj;
                    deleteDataByOject(mUserDao.loadAll().get(position));
//                    deleteData((long)position);
//                    mUrlList.remove(0);
//                    deleteAll();
                    refreshListView();
                    break;
            }


        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //database
        DaoSession daoSession = PingApplication.getPingApplication().getDaoSession();
        mUserDao = daoSession.getUserDao();
        initDatabase();
        //
        setupView();

    }

    private void initDatabase(){
        List<User> users = mUserDao.loadAll();
        if(users.size()==0){
            insertData("www.baidu.com");
            insertData("www.bing.com");
        }

    }

    private void setupView(){
        //
        mButtonRefesh = findViewById(R.id.refrashBaidu);
        mButtonRefesh.setOnClickListener(this);
        //
        mListView = findViewById(R.id.ping_list);
        mListViewAdapter = new ListViewAdapter(getApplicationContext(), getUrls());
        mListViewAdapter.setOnLongClick(new ListViewAdapter.OnItemLongClickLister() {
            @Override
            public void onItemLongClick(int position) {
                Message m = new Message();
                m.what=MSG_DELE_DATA;
                m.obj=position;
                mHandler.sendMessage(m);
            }
        });
        refreshListView();
        //
        mEditTextIP =findViewById(R.id.et_ip);
        mEditTextIP.clearFocus();
        Button buttonAdd = findViewById(R.id.btn_add);
        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mUrlList.add(mEditTextIP.getText().toString());
                insertData(mEditTextIP.getText().toString());
                refreshListView();
                mEditTextIP.setText("");
            }
        });
    }

    private void refreshListView(){
        mListView.invalidate();
        mListViewAdapter.updateDatas(getUrls());
        mListView.setAdapter(mListViewAdapter);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.refrashBaidu:
                refreshListView();
                break;
        }
    }

    private void insertData( String urlname) {
        int id = getUrls().size();
        Log.d(TAG, "insertData: urlname > id =" + id );
        User insertData = new User((long)id, urlname);
        try {
            mUserDao.insert(insertData);
        }catch (SQLiteConstraintException e){
            //repeat data
        }
    }

    private void deleteData(Long id){
        mUserDao.deleteByKey(1l);
        Log.w(TAG, "deleteData:id =  " + id );
    }

    private void deleteAll(){
        mUserDao.deleteAll();
    }

    private void deleteDataByOject(User user){
        Long id = user.getId();
        Log.w(TAG, "deleteData:id =  " + id );
        User userD = findUserById(id);
        mUserDao.delete(userD);
    }

    private void deleteDataById(Long id){
        Log.w(TAG, "deleteData:id =  " + id );
        User user = findUserById(id);
        mUserDao.delete(user);
    }
    /**
     * 查询指定ID闹钟记录
     **/
    public User findUserById(long id) {
        return mUserDao.queryBuilder().where(UserDao.Properties.Id.eq(id)).unique();
    }

    private List<String> getUrls() {
        List<User> users = mUserDao.loadAll();
        mUrlList.clear();
        Log.d(TAG, "getUrls: " + users.toString());
        for (int i = 0; i <users.size() ; i++) {
            Log.d(TAG, "getUrls: " + users.get(i).getUrlname());
            mUrlList.add(users.get(i).getUrlname());
        }
        return mUrlList;
    }
}
